package com.wisdom.benefit.jufa.product;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseActivity;
import com.wisdom.benefit.jufa.base.BaseApplication;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.CommonUtils;

import androidx.annotation.Nullable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AddAccountActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout cityRl;
    private TextView tv;
    private Button sureBtn, cancelBtn;
    private RadioGroup rg;
    private RadioButton rb1, rb2, rb3,rb4;
    private int passagewayType = 2;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 控件初始化
     */
    private void initView() {
        //选择地区
        cityRl = findViewById(R.id.add_account_city_rl);
        //地区
        tv = findViewById(R.id.add_account_address_tv);
        //确定按钮
        sureBtn = findViewById(R.id.add_account_sure_btn);
        //取消按钮
        cancelBtn = findViewById(R.id.add_account_cancel_btn);
        //单选框
        rg = findViewById(R.id.rgLayout);
        rb1 = findViewById(R.id.add_account_rbShi);
        rb2 = findViewById(R.id.rbFou);
        rb3 = findViewById(R.id.rbFif);
        rb4 = findViewById(R.id.rbsix);
        setClick();
    }

    /**
     * 设置监听
     */
    private void setClick() {
        //选择地区
        cityRl.setOnClickListener(this);
        //取消按钮
        cancelBtn.setOnClickListener(this);
        //确定按钮
        sureBtn.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.add_account_rbShi:
                    passagewayType = 2;
                    break;
                case R.id.rbFou:
                    passagewayType = 1;
                    break;
                case R.id.rbFif:
                    passagewayType = 3;
                    break;
                case R.id.rbsix:
                    passagewayType = 4;
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_account_city_rl:
                Intent intent = new Intent(this, ChooseCityActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.add_account_cancel_btn:
                finish();
                break;
            case R.id.add_account_sure_btn:
                passagewaySave(passagewayType, "", "");
                break;
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_add_account;
    }

    @Override
    protected void initViews() {
        initView();
    }

    @Override
    protected int getTitleStringRes() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String a = data.getStringExtra("city");
                    tv.setText(a);
                }
                break;
        }
    }

    /**
     * 新增固态通道
     */
    private void passagewaySave(int passagewayType, String passagewayName, String area) {
        addDisposable(RetrofitService.getNetService()
                .passagewaySave(Long.valueOf(BaseApplication.getContext().getUserId()).longValue(), passagewayType, passagewayName, area)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getCode() == 0) {
                        finish();
                    } else {
                        showBindToast(response.getMsg());
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    hideProgressDialog();
                    if (CommonUtils.isNetworkAvailable(getContext()))
                        showBindToast("新增失败");
                    else
                        showBindToast("无网络连接，请稍后再试");
                }));
    }
}
